package net.zoneland.zrdp.biz.controller.uniflow;


import net.zoneland.zrdp.biz.domain.pojo.form.CreateReadsForm;
import net.zoneland.zrdp.biz.domain.pojo.form.StartProcessForm;
import net.zoneland.zrdp.biz.domain.pojo.form.TaskProcessForm;
import net.zoneland.zrdp.biz.service.IFlowApproveInfoService;
import net.zoneland.zrdp.biz.validator.TaskProcessFormGroup;
import net.zoneland.zrdp.common.core.controller.BaseController;
import net.zoneland.zrdp.common.core.domain.AjaxResult;
import net.zoneland.zrdp.framework.web.service.uniflow.WorkFlowService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
 * 流程相关公共服务.
 *
 */

@Validated
@RestController
@RequestMapping("/biz/uniFlow")
public class WorkFlowController extends BaseController {

    @Resource
    private WorkFlowService workFlowService;
    @Resource
    private IFlowApproveInfoService flowApproveInfoService;

    /**
     * 查询流程模板上的节点配置信息.
     * @author YTG
     * @param businessProcess 业务流程名称.
     * @param sysNodeId 当前节点编码.
     * @param templateVer 流程模板的版本号.
     * @return NodeObject 节点配置信息
     **/
    @PostMapping("/queryProcessTemplateNode")
    public AjaxResult queryProcessTemplateNode(
        @NotBlank(message = "不能为空") final String businessProcess,
        @NotBlank(message = "不能为空") final String sysNodeId,
        final Integer templateVer) {
        return success(workFlowService.queryProcessTemplateNode(businessProcess, templateVer, sysNodeId));
    }

    /**
     * 获取指定工单流程实例对应的审批记录（包含未结束的待办记录）.
     * @author YTG
     * @param piid 流程实例id.
     * @param deptName 部门名称.
     * @return List<FlowApproveInfo> 审批记录
     **/
    @GetMapping("/getApproveList")
    public AjaxResult getApproveList(
        @NotBlank(message = "不能为空") final String piid, final String deptName) {
        return success(flowApproveInfoService.getTaskFlowApproveByPiid(piid, deptName));
    }

    // /**
    //  * 获取下一节点处理人.
    //  * @author YTG
    //  * @param approvePeopleForm 获取处理人参数对象.
    //  * @return List<UserRoleRel>
    //  **/
    // @PostMapping(value = "getApprovePeople")
    // public ResponseResult<List<UserRoleRel>> getApprovePeople(
    //     @RequestBody @Valid final ApprovePeopleForm approvePeopleForm) {
    //     return ResponseResult.okForLayUi(flowApproveInfoService.getApprovePeople(approvePeopleForm));
    // }

    /**
     * 起草. 流程启动
     * @author YTG
     * @param startProcessForm 流程启动参数对象.
     * @return List<TaskObject>
     **/
    @PostMapping("/draftTask")
    public AjaxResult draftTask(
        @RequestBody @Valid final StartProcessForm startProcessForm) {
        return success(flowApproveInfoService.draftTask(startProcessForm));
    }

    /**
     * 查询任务决策.
     * @param systemId 应用标识.
     * @param businessProcess 流程模板编码.
     * @param sysNodeId 节点编码.
     * @return List<TaskDecisionObject>
     **/
    @PostMapping(value = "queryTaskDecisions")
    public AjaxResult queryTaskDecisions(
        @NotBlank(message = "不能为空") final String systemId,
        @NotBlank(message = "不能为空") final String businessProcess,
        @NotBlank(message = "不能为空") final String sysNodeId) {
        return success(workFlowService.queryTaskDecisions(systemId, businessProcess, sysNodeId));
    }

    /**
     * 查询运行时任务决策.
     * @param piid 流程编号.
     * @param nodeId 节点编码.
     * @param extendData 扩展参数.
     * @return List<TaskDecisionObject>
     **/
    @PostMapping("/queryTaskRuntimeDecisions")
    public AjaxResult queryTaskRuntimeDecisions(
        @NotBlank(message = "不能为空") final String piid,
        @NotBlank(message = "不能为空") final String nodeId,
        final Map<String, String> extendData) {
        return success(workFlowService.queryTaskRuntimeDecisions(piid, nodeId, extendData));
    }

    /**
     * 根据piid删除流程.
     * @param piid 流程编号.
     * @param notion 申请原因.
     * @return Boolean
     **/
    @GetMapping(value = "deleteProcess")
    public AjaxResult deleteProcess(
        @NotBlank(message = "不能为空") final String piid,
        final String notion) {
        return success(workFlowService.deleteProcess(piid, null, notion));
    }

    /**
     * 任务转交.
     * @param taskProcessForm 任务处理参数对象.
     * @return List<TaskObject>
     **/
    @PostMapping(value = "transferTask")
    public AjaxResult transferTask(
        @RequestBody @Valid final TaskProcessForm taskProcessForm) {
        return success(flowApproveInfoService.transferTask(taskProcessForm));
    }

    /**
     * 创建阅办.
     * @param createReadsForm 创建阅办参数对象.
     * @return List<ReadObject>
     **/
    @PostMapping(value = "createReads")
    public AjaxResult createReads(
        @RequestBody @Valid final CreateReadsForm createReadsForm) {
        return success(flowApproveInfoService.createReads(createReadsForm));
    }

    /**
     * 完成阅办.
     * @param riid 待阅任务编码.
     * @param notion 审批意见.
     * @return Integer
     **/
    @GetMapping(value = "completeRead")
    public AjaxResult completeRead(
        @NotBlank(message = "不能为空") final String riid, final String notion) {
        return success(workFlowService.completeRead(riid, notion));
    }

    /**
     * 任务处理.
     * @param taskProcessForm 任务处理参数对象.
     * @return net.zoneland.demo.common.bo.ResponseResult<java.util.List<net.zoneland.uniflow.client.TaskObject>>
     **/
    @PostMapping(value = "completeTask")
    public AjaxResult completeTask(
        @RequestBody @Validated(value = TaskProcessFormGroup.DecisionGroup.class)
        final TaskProcessForm taskProcessForm) {
        return success(flowApproveInfoService.completeTask(taskProcessForm));
    }

    /**
     * 设置流程扩展参数.
     * @param piid 流程编码.
     * @param extendData 扩展参数.
     * @return Integer
     **/
    @PostMapping(value = "setExtendData")
    public AjaxResult setExtendData(
        @NotBlank(message = "不能为空") final String piid,
        @NotNull(message = "不能为空") final Map<String, String> extendData) {
        return success(workFlowService.setExtendData(piid, extendData));
    }
}
